package com.ymatou.iapi.sellertrading.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellertrading.service.getorderfundflowCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.database.model.YmtOrders;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;



public class Ts_getorderfundflow {	
	private static List<YmtOrders> info;
//	private static YMatouSellerOrderWapper sellerorderwapper;

	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取订单资金明细");
		try {
			
//			sellerorderwapper = new YMatouSellerOrderWapper();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new getorderfundflowCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_001() {
		Logger.start(true, "获取订单资金明细 - 未付款订单");
		try {
			info = new YmtOrdersWapper().selectReceivedLastOrder(1);
			
			int buyerid = info.get(0).getIbuyerid();
			int orderid = info.get(0).getIorderid();
			int userid = info.get(0).getIuserid();
			
			//sellertrading iapi ymatou com接口
			Logger.comment("************************调用【sellertrading】服务订单资金明细接口做为实际结果***********************");
			JSONObject getorderfundflowResult = new getorderfundflowCall(buyerid,orderid,userid).callAndGetReturnData();
			Logger.verifyEquals(200,getorderfundflowResult.getInt("Code"), "响应报文-返回码");
			
			getorderfundflowCall getorderfundflowCall = new getorderfundflowCall();
			
			Logger.comment("************************调用底层订单资金明细接口做为期待结果取出************************");
			//api.trading.seller.query.ymatou.com 接口
			JSONObject getOrderFundDetailResult = getorderfundflowCall.getOrderFundDetail(orderid, true);
			
			Logger.comment("************************进行断言************************");
			//断言(sellertrading接口的返回  与 调用的底层接口的返回 直接进行对比断言)
			veracity(getorderfundflowResult, getOrderFundDetailResult);
						
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_002() {
		Logger.start(true, "获取订单资金明细 - 已付款等待发货订单");
		try {
			info = new YmtOrdersWapper().selectReceivedLastOrder(2);
			
			int buyerid = info.get(0).getIbuyerid();
			int orderid = info.get(0).getIorderid();
			int userid = info.get(0).getIuserid();
			
			//sellertrading iapi ymatou com接口
			Logger.comment("************************调用【sellertrading】服务订单资金明细接口做为实际结果***********************");
			JSONObject getorderfundflowResult = new getorderfundflowCall(buyerid,orderid,userid).callAndGetReturnData();
			Logger.verifyEquals(200,getorderfundflowResult.getInt("Code"), "响应报文-返回码");
			
			getorderfundflowCall getorderfundflowCall = new getorderfundflowCall();
			
			Logger.comment("************************调用底层订单资金明细接口做为期待结果取出************************");
			//api.trading.seller.query.ymatou.com 接口
			JSONObject getOrderFundDetailResult = getorderfundflowCall.getOrderFundDetail(orderid, true);
			
			Logger.comment("************************进行断言************************");
			//断言(sellertrading接口的返回  与 调用的底层接口的返回 直接进行对比断言)
			veracity(getorderfundflowResult, getOrderFundDetailResult);
						
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_003() {
		Logger.start(true, "获取订单资金明细 - 已发货订单");
		try {
			info = new YmtOrdersWapper().selectReceivedLastOrder(3);
			
			int buyerid = info.get(0).getIbuyerid();
			int orderid = info.get(0).getIorderid();
			int userid = info.get(0).getIuserid();
			
			//sellertrading iapi ymatou com接口
			Logger.comment("************************调用【sellertrading】服务订单资金明细接口做为实际结果***********************");
			JSONObject getorderfundflowResult = new getorderfundflowCall(buyerid,orderid,userid).callAndGetReturnData();
			Logger.verifyEquals(200,getorderfundflowResult.getInt("Code"), "响应报文-返回码");
			
			getorderfundflowCall getorderfundflowCall = new getorderfundflowCall();
			
			Logger.comment("************************调用底层订单资金明细接口做为期待结果取出************************");
			//api.trading.seller.query.ymatou.com 接口
			JSONObject getOrderFundDetailResult = getorderfundflowCall.getOrderFundDetail(orderid, true);
			
			Logger.comment("************************进行断言************************");
			//断言(sellertrading接口的返回  与 调用的底层接口的返回 直接进行对比断言)
			veracity(getorderfundflowResult, getOrderFundDetailResult);
						
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_004() {
		Logger.start(true, "获取订单资金明细 - 确认收货订单");
		try {
			info = new YmtOrdersWapper().selectReceivedLastOrder(4);
			
			int buyerid = info.get(0).getIbuyerid();
			int orderid = info.get(0).getIorderid();
			int userid = info.get(0).getIuserid();
			
			//sellertrading iapi ymatou com接口
			Logger.comment("************************调用【sellertrading】服务订单资金明细接口做为实际结果***********************");
			JSONObject getorderfundflowResult = new getorderfundflowCall(buyerid,orderid,userid).callAndGetReturnData();
			Logger.verifyEquals(200,getorderfundflowResult.getInt("Code"), "响应报文-返回码");
			
			getorderfundflowCall getorderfundflowCall = new getorderfundflowCall();
			
			Logger.comment("************************调用底层订单资金明细接口做为期待结果取出************************");
			//api.trading.seller.query.ymatou.com 接口
			JSONObject getOrderFundDetailResult = getorderfundflowCall.getOrderFundDetail(orderid, true);
			
			Logger.comment("************************进行断言************************");
			//断言(sellertrading接口的返回  与 调用的底层接口的返回 直接进行对比断言)
			veracity(getorderfundflowResult, getOrderFundDetailResult);
						
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_005() {
		Logger.start(true, "获取订单资金明细 - 用户接受取消交易订单");
		try {
			info = new YmtOrdersWapper().selectReceivedLastOrder(12);
			
			int buyerid = info.get(0).getIbuyerid();
			int orderid = info.get(0).getIorderid();
			int userid = info.get(0).getIuserid();
			
			//sellertrading iapi ymatou com接口
			Logger.comment("************************调用【sellertrading】服务订单资金明细接口做为实际结果***********************");
			JSONObject getorderfundflowResult = new getorderfundflowCall(buyerid,orderid,userid).callAndGetReturnData();
			Logger.verifyEquals(200,getorderfundflowResult.getInt("Code"), "响应报文-返回码");
			
			getorderfundflowCall getorderfundflowCall = new getorderfundflowCall();
			
			Logger.comment("************************调用底层订单资金明细接口做为期待结果取出************************");
			//api.trading.seller.query.ymatou.com 接口
			JSONObject getOrderFundDetailResult = getorderfundflowCall.getOrderFundDetail(orderid, true);
			
			Logger.comment("************************进行断言************************");
			//断言(sellertrading接口的返回  与 调用的底层接口的返回 直接进行对比断言)
			veracity(getorderfundflowResult, getOrderFundDetailResult);
						
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_006() {
		Logger.start(true, "获取订单资金明细 - 卖家接受取消交易订单");
		try {
			info = new YmtOrdersWapper().selectReceivedLastOrder(13);
			
			int buyerid = info.get(0).getIbuyerid();
			int orderid = info.get(0).getIorderid();
			int userid = info.get(0).getIuserid();
			
			//sellertrading iapi ymatou com接口
			Logger.comment("************************调用【sellertrading】服务订单资金明细接口做为实际结果***********************");
			JSONObject getorderfundflowResult = new getorderfundflowCall(buyerid,orderid,userid).callAndGetReturnData();
			Logger.verifyEquals(200,getorderfundflowResult.getInt("Code"), "响应报文-返回码");
			
			getorderfundflowCall getorderfundflowCall = new getorderfundflowCall();
			
			Logger.comment("************************调用底层订单资金明细接口做为期待结果取出************************");
			//api.trading.seller.query.ymatou.com 接口
			JSONObject getOrderFundDetailResult = getorderfundflowCall.getOrderFundDetail(orderid, true);
			
			Logger.comment("************************进行断言************************");
			//断言(sellertrading接口的返回  与 调用的底层接口的返回 直接进行对比断言)
			veracity(getorderfundflowResult, getOrderFundDetailResult);
						
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_007() {
		Logger.start(true, "获取订单资金明细 - 卖家确认订单");
		try {
			info = new YmtOrdersWapper().selectReceivedLastOrder(17);
			
			int buyerid = info.get(0).getIbuyerid();
			int orderid = info.get(0).getIorderid();
			int userid = info.get(0).getIuserid();
			
			//sellertrading iapi ymatou com接口
			Logger.comment("************************调用【sellertrading】服务订单资金明细接口做为实际结果***********************");
			JSONObject getorderfundflowResult = new getorderfundflowCall(buyerid,orderid,userid).callAndGetReturnData();
			Logger.verifyEquals(200,getorderfundflowResult.getInt("Code"), "响应报文-返回码");
			
			getorderfundflowCall getorderfundflowCall = new getorderfundflowCall();
			
			Logger.comment("************************调用底层订单资金明细接口做为期待结果取出************************");
			//api.trading.seller.query.ymatou.com 接口
			JSONObject getOrderFundDetailResult = getorderfundflowCall.getOrderFundDetail(orderid, true);
			
			Logger.comment("************************进行断言************************");
			//断言(sellertrading接口的返回  与 调用的底层接口的返回 直接进行对比断言)
			veracity(getorderfundflowResult, getOrderFundDetailResult);
						
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_008() {
		Logger.start(true, "获取订单资金明细 - 系统自动取消交易");
		try {
			info = new YmtOrdersWapper().selectReceivedLastOrder(18);
			
			int buyerid = info.get(0).getIbuyerid();
			int orderid = info.get(0).getIorderid();
			int userid = info.get(0).getIuserid();
			
			//sellertrading iapi ymatou com接口
			Logger.comment("************************调用【sellertrading】服务订单资金明细接口做为实际结果***********************");
			JSONObject getorderfundflowResult = new getorderfundflowCall(buyerid,orderid,userid).callAndGetReturnData();
			Logger.verifyEquals(200,getorderfundflowResult.getInt("Code"), "响应报文-返回码");
			
			getorderfundflowCall getorderfundflowCall = new getorderfundflowCall();
			
			Logger.comment("************************调用底层订单资金明细接口做为期待结果取出************************");
			//api.trading.seller.query.ymatou.com 接口
			JSONObject getOrderFundDetailResult = getorderfundflowCall.getOrderFundDetail(orderid, true);
			
			Logger.comment("************************进行断言************************");
			//断言(sellertrading接口的返回  与 调用的底层接口的返回 直接进行对比断言)
			veracity(getorderfundflowResult, getOrderFundDetailResult);
						
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_009() {
		Logger.start(false, "订单不存在");
		try {
			
			//sellertrading iapi ymatou com接口
			JSONObject getorderfundflowResult = new getorderfundflowCall(3383,123123123,4085).callAndGetReturnData();
			Logger.verifyEquals(200,getorderfundflowResult.getInt("Code"), "响应报文-返回码");
			
			Logger.verifyEquals("false",getorderfundflowResult.getJSONObject("Data").getString("OnlyNeedPayOnce"), "OnlyNeedPayOnce验证为false");

			Logger.verifyEquals("null",getorderfundflowResult.getJSONObject("Data").getString("Flows"), "Flows部分应为空");
			Logger.verifyEquals("null",getorderfundflowResult.getJSONObject("Data").getString("Summary"), "Summary部分应为空");
						
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_010() {
		Logger.start(false, "订单为0");
		try {
			
			//sellertrading iapi ymatou com接口
			JSONObject getorderfundflowResult = new getorderfundflowCall(3383,0,4085).callAndGetReturnData();
			Logger.verifyEquals(201,getorderfundflowResult.getInt("Code"), "响应报文-返回码");			
						
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_011() {
		Logger.start(false, "卖家不正确");
		try {
			
			info = new YmtOrdersWapper().selectReceivedLastOrder(4);
			
			int buyerid = info.get(0).getIbuyerid();
			int orderid = info.get(0).getIorderid();
			int userid = info.get(0).getIuserid();
			
			//sellertrading iapi ymatou com接口
			Logger.comment("************************调用【sellertrading】服务订单资金明细接口做为实际结果***********************");
			JSONObject getorderfundflowResult = new getorderfundflowCall(11,orderid,userid).callAndGetReturnData();
			Logger.verifyEquals(200,getorderfundflowResult.getInt("Code"), "响应报文-返回码");
			
			getorderfundflowCall getorderfundflowCall = new getorderfundflowCall();
			
			Logger.comment("************************调用底层订单资金明细接口做为期待结果取出************************");
			//api.trading.seller.query.ymatou.com 接口
			JSONObject getOrderFundDetailResult = getorderfundflowCall.getOrderFundDetail(orderid, true);
			
			Logger.comment("************************进行断言************************");
			//断言(sellertrading接口的返回  与 调用的底层接口的返回 直接进行对比断言)
			veracity(getorderfundflowResult, getOrderFundDetailResult);
						
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}	
		
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_012() {
		Logger.start(false, "买家不正确");
		try {
			
			info = new YmtOrdersWapper().selectReceivedLastOrder(4);
			
			int buyerid = info.get(0).getIbuyerid();
			int orderid = info.get(0).getIorderid();
			int userid = info.get(0).getIuserid();
			
			//sellertrading iapi ymatou com接口
			Logger.comment("************************调用【sellertrading】服务订单资金明细接口做为实际结果***********************");
			JSONObject getorderfundflowResult = new getorderfundflowCall(buyerid,orderid,11).callAndGetReturnData();
			Logger.verifyEquals(200,getorderfundflowResult.getInt("Code"), "响应报文-返回码");
			
			getorderfundflowCall getorderfundflowCall = new getorderfundflowCall();
			
			Logger.comment("************************调用底层订单资金明细接口做为期待结果取出************************");
			//api.trading.seller.query.ymatou.com 接口
			JSONObject getOrderFundDetailResult = getorderfundflowCall.getOrderFundDetail(orderid, true);
			
			Logger.comment("************************进行断言************************");
			//断言(sellertrading接口的返回  与 调用的底层接口的返回 直接进行对比断言)
			veracity(getorderfundflowResult, getOrderFundDetailResult);
						
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}		
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_013() {
		Logger.start(false, "买家id为0");
		try {
			
			info = new YmtOrdersWapper().selectReceivedLastOrder(4);
			
			int buyerid = info.get(0).getIbuyerid();
			int orderid = info.get(0).getIorderid();
			int userid = info.get(0).getIuserid();
			
			//sellertrading iapi ymatou com接口
			JSONObject getorderfundflowResult = new getorderfundflowCall(buyerid,orderid,0).callAndGetReturnData();
			Logger.verifyEquals(201,getorderfundflowResult.getInt("Code"), "响应报文-返回码");
			
			Logger.verifyEquals("用户信息错误",getorderfundflowResult.getString("Msg"), "响应报文-Msg");

		} catch (Exception e) {
			 Logger.fail(e);
		}
	}	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_noneedpostpay_014() {
		Logger.start(false, "卖家id为0");
		try {
			
			info = new YmtOrdersWapper().selectReceivedLastOrder(4);
			
			int buyerid = info.get(0).getIbuyerid();
			int orderid = info.get(0).getIorderid();
			int userid = info.get(0).getIuserid();
			
			//sellertrading iapi ymatou com接口
			JSONObject getorderfundflowResult = new getorderfundflowCall(0,orderid,userid).callAndGetReturnData();
			Logger.verifyEquals(201,getorderfundflowResult.getInt("Code"), "响应报文-返回码");
			
			Logger.verifyEquals("用户信息错误",getorderfundflowResult.getString("Msg"), "响应报文-Msg");

		} catch (Exception e) {
			 Logger.fail(e);
		}
	}	
		
		
	
	
	
	
	
	
	
	
	
	
	/**
	 * 获取订单资金明细接口 断言方法
	 * @param getorderfundflowResult
	 * @param getOrderFundDetailResult
	 * @throws Exception
	 */
	private static void veracity(JSONObject getorderfundflowResult , JSONObject getOrderFundDetailResult) throws  Exception {
		
		
		//映射getorderfundflow 与 getOrderFundDetail 的关系
		Map<String, String> mapping = new HashMap<String,String>();
		mapping.put("Data->OnlyNeedPayOnce", "Data->OnlyNeedPayOnce");
		
		//Flow部分
		if (getorderfundflowResult.getJSONObject("Data").getJSONArray("Flows").length() != 0 &  getOrderFundDetailResult.getJSONObject("Data").getJSONArray("Flows").length() != 0  ) {

			mapping.put("Data->Flows[0]->AccountPaidAmount", "Data->Flows[0]->AccountPaidAmount");
			mapping.put("Data->Flows[0]->ThirdPartyPaidAmount", "Data->Flows[0]->ThirdPartyPaidAmount");
			mapping.put("Data->Flows[0]->ThirdPartyName", "Data->Flows[0]->ThirdPartyName");
			mapping.put("Data->Flows[0]->IsIncoming", "Data->Flows[0]->IsIncoming");
			mapping.put("Data->Flows[0]->Operation", "Data->Flows[0]->Operation");
			mapping.put("Data->Flows[0]->Evidence", "Data->Flows[0]->Evidence");
			mapping.put("Data->Flows[0]->CouponChannel", "Data->Flows[0]->CouponChannel");
			mapping.put("Data->Flows[0]->CouponCode", "Data->Flows[0]->CouponCode");
			mapping.put("Data->Flows[0]->SellerCouponCode", "Data->Flows[0]->SellerCouponCode");
			mapping.put("Data->Flows[0]->FreeCard", "Data->Flows[0]->FreeCard");
			mapping.put("Data->Flows[0]->PriceChange", "Data->Flows[0]->PriceChange");
			mapping.put("Data->Flows[0]->Total", "Data->Flows[0]->Total");
			mapping.put("Data->Flows[0]->Cash", "Data->Flows[0]->Cash");
			mapping.put("Data->Flows[0]->Gift", "Data->Flows[0]->Gift");
			mapping.put("Data->Flows[0]->Coupon", "Data->Flows[0]->Coupon");
			mapping.put("Data->Flows[0]->SellerCoupon", "Data->Flows[0]->SellerCoupon");
			mapping.put("Data->Flows[0]->Freight", "Data->Flows[0]->Freight");
			mapping.put("Data->Flows[0]->ThirdPartyDiscount", "Data->Flows[0]->ThirdPartyDiscount");			
		}
		
		//Summary部分
		mapping.put("Data->Summary->PaidCash", "Data->Summary->PaidCash");
		mapping.put("Data->Summary->PaidGift", "Data->Summary->PaidGift");
		mapping.put("Data->Summary->PaidFreeCard", "Data->Summary->PaidFreeCard");
		mapping.put("Data->Summary->PaidYmtCoupon", "Data->Summary->PaidYmtCoupon");
		mapping.put("Data->Summary->PaidSellerCoupon", "Data->Summary->PaidSellerCoupon");
		mapping.put("Data->Summary->PostPaidCash", "Data->Summary->PostPaidCash");
		mapping.put("Data->Summary->PostPaidGift", "Data->Summary->PostPaidGift");
		mapping.put("Data->Summary->TotalThirdPartyDiscount", "Data->Summary->TotalThirdPartyDiscount");
		mapping.put("Data->Summary->TotalProductPrice", "Data->Summary->TotalProductPrice");
		mapping.put("Data->Summary->FirstPayProductPrice", "Data->Summary->FirstPayProductPrice");
		mapping.put("Data->Summary->Freight", "Data->Summary->Freight");
		mapping.put("Data->Summary->OrderDiscount", "Data->Summary->OrderDiscount");
		mapping.put("Data->Summary->PostPayDiscount", "Data->Summary->PostPayDiscount");
		mapping.put("Data->Summary->TotalRefundedCash", "Data->Summary->TotalRefundedCash");
		mapping.put("Data->Summary->TotalRefundedGift", "Data->Summary->TotalRefundedGift");
		mapping.put("Data->Summary->TotalRefundedYmtCoupon", "Data->Summary->TotalRefundedYmtCoupon");
		mapping.put("Data->Summary->TotalActivityPrice", "Data->Summary->TotalActivityPrice");
		mapping.put("Data->Summary->TotalCanceledCash", "Data->Summary->TotalCanceledCash");
		mapping.put("Data->Summary->TotalCanceledYmtCoupon", "Data->Summary->TotalCanceledYmtCoupon");
		mapping.put("Data->Summary->TotalRefundedThirdPartyDiscount", "Data->Summary->TotalRefundedThirdPartyDiscount");
		
		//批量断言
		AssertService.assertResultEqual(mapping, getorderfundflowResult, getOrderFundDetailResult);	
		
		//对比Flow部分数量
		Logger.verifyEquals(getOrderFundDetailResult.getJSONObject("Data").getJSONArray("Flows").length(), 
				getorderfundflowResult.getJSONObject("Data").getJSONArray("Flows").length(), "对比Flow部分数量");
				
	}			
	
}
